import { ref } from 'vue'
import type { BasicColumn, FormSchema } from '@/components/Table'
import { useRender } from '@/components/Table'
import { deptApi } from '@/api/sys/deptApi'
import { useDictStoreWithout } from '@/store/modules/dict'

export const columns: BasicColumn[] = [
  {
    title: '名称',
    dataIndex: 'deptName',
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: 40,
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '状态',
    dataIndex: 'enabled',
    width: 60,
    customRender: ({ value }) => useRender.renderDictTag(value, 'STATE'),
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: '名称',
    field: 'deptName',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '状态',
    field: 'enabled',
    component: 'Select',
    componentProps: {
      options: useDictStoreWithout().getDictOptions('STATE'),
    },
    colProps: { span: 8 },
  },
]

const params = ref({})
export function setDeptTreeParams(model: Recordable) {
  params.value = model
  return params
}

export const formSchema: FormSchema[] = [
  {
    field: 'deptId',
    show: false,
    component: 'Input',
  },
  {
    label: '上级',
    field: 'parentDeptId',
    required: false,
    component: 'ApiTreeSelect',
    componentProps: {
      params,
      api: deptApi.tree,
      parentFiled: 'parentDeptId',
      valueField: 'deptId',
      labelField: 'deptName',
      resultField: 'rows',
    },
  },
  {
    label: '名称',
    field: 'deptName',
    required: true,
    component: 'Input',
  },
  {
    label: '显示顺序',
    field: 'sort',
    required: true,
    defaultValue: 0,
    component: 'InputNumber',
  },
  {
    label: '启用状态',
    field: 'enabled',
    required: true,
    component: 'Switch',
    defaultValue: true,
  },
  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    required: false,
    componentProps: {
      placeholder: '请输入备注',
    },
  },
]
